#-------------------------------------------------------------------------------
# Author:   A.C. Verbeck
# Date:     3:54 PM Monday, January 6, 2014
# Summary:  Google maps REST API test
# Description:
#
#-------------------------------------------------------------------------------
import sys
import json
import urllib
import urllib2
import response

#-------------------------------------------------------------------------------
# Google Maps definitions
#   URL to the API
#   Other google map information (origin, destination, sensor)
#-------------------------------------------------------------------------------
url = 'http://maps.googleapis.com/maps/api/directions/json'

"""
values = {
    'origin' : '1005 Burnside St, Portland, OR 97209',
    'destination' : '810 Van Buren St, Oregon City, OR 97045',
    'sensor' : 'false'
}
values = {
    'origin' : '5200 NE Elam Young Parkway, Hillsboro, OR 97124',
    'destination' : '810 Van Buren St, Oregon City, OR 97045',
    'sensor' : 'false'
}
"""
values = {
    'origin' : '3717 Via Pacifica Walk, Oxnard, CA',
    'destination' : '810 Van Buren St, Oregon City, OR 97045',
    'sensor' : 'false'
}

#-------------------------------------------------------------------------------
#   MyHTMLParser:
#   This very simpler parser clears out unnecessary tag information
#   from the Google directions.
#-------------------------------------------------------------------------------
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'div':
            print
    def handle_data(self, data):
        sys.stdout.write(data)

#-------------------------------------------------------------------------------
# This is done to ensure that a "get" is done instead of a "post".
#-------------------------------------------------------------------------------
url_values = urllib.urlencode(values)
full_url = url + '?' + url_values

#-------------------------------------------------------------------------------
# Send the 'get' request for directions
#-------------------------------------------------------------------------------
try:
    response = urllib2.urlopen(full_url)
except HTTPError as e:
    print 'The server couldn\'t fulfill the request.'
    print 'Error code: ', e.code
except URLError as e:
    print 'We failed to reach a server.'
    print 'Reason: ', e.reason

json_dir = response.read()

#-------------------------------------------------------------------------------
# Interpret the JSON response 
#-------------------------------------------------------------------------------
data = json.loads(json_dir.decode('utf8'))
print data['routes'][0]['legs'][0]['start_address']
print data['routes'][0]['legs'][0]['end_address']
print data['routes'][0]['legs'][0]['distance']['text']

h = MyHTMLParser()
for steps in data['routes'][0]['legs'][0]['steps']:
    h.feed(steps['html_instructions'])
    print

print data['status']

#
#   End maps_goog.py
#
